package com.ybfenpan.controller.admin.shop;


import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.shop.Goods;
import com.ybfenpan.service.shop.GoodsService;
import com.ybfenpan.utils.FileUtils;
import com.ybfenpan.utils.MapUtil;
import com.ybfenpan.utils.SysConst;
import com.ybfenpan.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;





/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class GoodsShowController extends BaseController {
    @Resource
    private GoodsService goodsService;
   
    
    @RequestMapping("admin/shop/goodsShow.shtml")
    public String show(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Goods goods = goodsService.getByKey(id);
        	  
    		request.setAttribute("goods",goods);
             
            formMap.put("id", id);
             
            
        }
        return "admin/shop/goodsShow";
       
    } 
     
    
    
    @RequestMapping("admin/shop/goodsUpdate.shtml")
    public String add(@RequestParam MultipartFile file, ModelMap model,HttpServletRequest request, HttpServletResponse response) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        String title = MapUtil.getString(formMap, "title");
    	String detail = MapUtil.getString(formMap, "detail");
    	Integer totalNum = MapUtil.getIntValue(formMap, "totalNum");
    	Double price= MapUtil.getDouble(formMap, "price");
    	boolean result = true;
        if(0 < id){
        	if(file.getOriginalFilename()==null||file.getOriginalFilename()==""){
    			 
    			Goods goods =this.goodsService.getByKey(id);
    			goods.setTitle(title);
    			goods.setDetail(detail);
    			goods.setTotalNum(totalNum);
    			 
    			goods.setPrice(price);
    			 
                goodsService.update(goods);
    			
    		}else{
    			
    			
            	
            	String fileName = file.getOriginalFilename();
            	String docName =  FileUtils.getUUIDFileName(fileName);
            	//复制保存到E盘
                String newFile = SysConst.TMP_FILE + docName;
                
                
                String fileContentType=file.getContentType();
                //后缀名
    			String ext = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
    		
    			List<String> arrowType = Arrays.asList("images/gif","images/jpg","images/jpeg","images/pjpeg","images/png","images/bmp","image/gif","image/jpg","image/jpeg","image/pjpeg","image/png","image/bmp","image/x-png");
    			List<String> arrowExt = Arrays.asList("gif","jpg","jpeg","png","bmp");
    			
    			 
    			if(!(arrowType.contains(fileContentType.toLowerCase()) && arrowExt.contains(ext))){
    				 
    				setMessage(model,"上传文件只接收gif/jpg/jpeg/png/bmp类型,您的文件类型为！"+fileContentType);
    				result = false;
    			}else{
    				
    				if(file.getSize()>(4*1024*1024)){
    					
    					setMessage(model,"上传图片不能大于4M。");
    					result = false;
    				}
    				//文件存储到硬盘
            		boolean flag = FileUtils.createFileToDisk(newFile, file.getInputStream());
    				 
                    if(flag){
                        String tmpFile = Tools.getWebPath(this.getClass())+SysConst.TMP_FILE+docName;
                        FileUtils.copyFile(newFile,tmpFile);
                        
                        String path = request.getContextPath();
                    	String port = (0<request.getServerPort()) ? (":"+request.getServerPort()) : "";
                    	String basePath = request.getScheme()+"://"+request.getServerName()+port+path+"/";
                        
                    	Random random = new Random(); 
        				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); 
        				String goodsNo="P"+formatter.format(new Date()) + random.nextInt(2) ;
                        
                        Goods goods = this.goodsService.getByKey(id);
            			goods.setTitle(title);
            			goods.setDetail(detail);
            			goods.setTotalNum(totalNum);
            			goods.setImgName(docName);
            			goods.setImgPath(basePath+SysConst.TMP_FILE);
            			goods.setPrice(price);
            			goods.setGoodsNo(goodsNo);
                        goodsService.update(goods);
                         
                        return "redirect:goodsList.shtml";
                    }else{
                    	 
                    	setMessage(model,"上传图片失败");
                    	result = false;
                    }
                    
    			}
    				
    		     
    		}
        	
        	if(result){
        		setMessage(model,"更新成功！");
        		
    		} 
        	
            
    		request.setAttribute("result", result);
    		Goods goods= goodsService.getByKey(id);
            request.setAttribute("goods",goods);
            
        }
        return "admin/shop/goodsShow";
    }

}
